package com.huaruan.youdi.views.dialogs;

import android.app.Dialog;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.huaruan.youdi.R;
import com.huaruan.youdi.utils.TimeUtils;
import com.huaruan.youdi.views.widget.ActionListener;

import java.util.Calendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.defaults.view.DateTimePickerView;

public class DatePickerDialog extends BaseDialogFragment {
    @BindView(R.id.ll_dismiss)
    LinearLayout ll_dismiss;
    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    @BindView(R.id.datePicker)
    DateTimePickerView datePicker;

    ActionListener listener;

    @Override
    public void initView(View contentView, Dialog dialog) {
        ButterKnife.bind(this, contentView);
        ll_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String date = getDateString(datePicker.getSelectedDate());
                String[] arr = date.split("-");
                if (arr.length > 1) {
                    listener.onDoneClick(arr[0], arr[1]);
                }
                dismiss();
            }
        });
        datePicker.getDayPickerView().setVisibility(View.GONE);
    }


    public void setActionListener(ActionListener listener) {
        this.listener = listener;
    }

    @Override
    public int contentViewResId() {
        return R.layout.dialog_date;
    }

    @Override
    public Boolean isFullScreen() {
        return true;
    }

    private String getDateString(Calendar date) {
        int year = date.get(Calendar.YEAR);
        int month = date.get(Calendar.MONTH);
        int dayOfMonth = date.get(Calendar.DAY_OF_MONTH);
        return String.format(Locale.getDefault(), "%d-%02d-%02d", year, month + 1, dayOfMonth);
    }

}
